package com.levi.test.init;

import com.levi.test.rpc.RpcServer;
import com.levi.test.constant.ExecuteCommonData;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class ExecuteInit {

    private Thread thread;

    @Value("#{'${nopro:local,dev,live,trunk,stress,pre,test,develop}'.split(',')}")
    private Set<String> noProSet;

    @PostConstruct
    private void init() {
        String active = System.getProperty("spring.profiles.active");
        if (active == null || noProSet.contains(active)) {
            thread = new Thread(() -> {
                new RpcServer(ExecuteCommonData.rpcPort).start();
            });
            thread.setDaemon(true);
            thread.start();
        }
    }

    @PreDestroy
    private void end() {
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}
